package be.kuleuven.mume.listProxys;

import java.util.TreeMap;

import be.kuleuven.mume.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

public class DeviderProxy extends DataSetObserver implements ListAdapter{
	private static int DEVIDER_TYPE = 0;
	private static int COUNT_TYPES = 1;
	
	private Context context;
	private TreeMap<Integer,Devidable> deviders = new TreeMap<Integer,Devidable>();
	private ArrayAdapter<? extends Devidable> adapter;
	
	public interface Devidable{
		public String getDevider(Context context);
	}
	
	public DeviderProxy(Context context, ArrayAdapter<? extends Devidable> adapter) {
		this.context = context;
		this.adapter = adapter;
		adapter.registerDataSetObserver(this);
		updateDeviders();
	}
	
	@Override
	public void onChanged(){
		updateDeviders();
	}
	
	private void updateDeviders() {
		String devider = "";
		int countDev = 0;
		deviders.clear();
		for(int i = 0 ; i < adapter.getCount() ; i++){
			if(!adapter.getItem(i).getDevider(context).equals(devider)){
				deviders.put(i + countDev, adapter.getItem(i));
				devider = adapter.getItem(i).getDevider(context);
				countDev++;
			}
		}
		
	}
	
	public int getCount() {
		return adapter.getCount() + deviders.size();
	}

	public Object getItem(int position) {
		if(deviders.containsKey(position))
			return deviders.get(position);
		
		int countDev = deviders.headMap(position).size();
		return adapter.getItem(position-countDev);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		if(deviders.containsKey(position))
			return DEVIDER_TYPE;
		else{
			int countDev = deviders.headMap(position).size();
			return COUNT_TYPES + adapter.getItemViewType(position - countDev);
		}
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if(getItemViewType(position) == DEVIDER_TYPE) {
			if(convertView == null) {
				LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = vi.inflate(R.layout.rij_sepperator, null);
			}
			TextView textv = (TextView) convertView.findViewById(R.id.DateSepp);
			if (textv != null) {
				textv.setText(deviders.get(position).getDevider(context));
			}
			return convertView;
		}
		int countDev = deviders.headMap(position).size();
		return adapter.getView(position - countDev, convertView, parent);
	}

	public int getViewTypeCount() {
		return COUNT_TYPES + adapter.getViewTypeCount();
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return deviders.isEmpty() && adapter.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		adapter.registerDataSetObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		adapter.unregisterDataSetObserver(observer);
	}

	public boolean areAllItemsEnabled() {
		return false;
	}

	public boolean isEnabled(int arg0) {
		if(deviders.containsKey(arg0))
			return false;
		int countDev = deviders.headMap(arg0).size();
		return adapter.isEnabled(arg0 - countDev);
	}

}
